package com.robaone.gwtgenerator.client;

import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.robaone.gwtgenerator.client.data.GWTData;

public class FramesUI extends Composite {
	private HTML html;
	private TextArea textArea;
	private HTML code;
	private SplitLayoutPanel splitLayoutPanel;

	public FramesUI(int clientheight,int clientwidth) {
		
		splitLayoutPanel = new SplitLayoutPanel();
		initWidget(splitLayoutPanel);
		
		ScrollPanel codePanel = new ScrollPanel();
		splitLayoutPanel.addEast(codePanel, clientwidth/2);
		codePanel.setHeight("100%");
		codePanel.setWidth("100%");
		code = new HTML("<pre>java code;</pre>", true);
		codePanel.add(code);
		
		FlowPanel inputPanel = new FlowPanel();
		inputPanel.setHeight("100%");
		splitLayoutPanel.addNorth(inputPanel, clientheight * 0.3);
		
		textArea = new TextArea();
		inputPanel.add(textArea);
		textArea.setWidth("100%");
		
		textArea.setHeight("100%");
		
		ScrollPanel htmlPreviewPanel = new ScrollPanel();
		htmlPreviewPanel.setSize("100%", "100%");
		splitLayoutPanel.add(htmlPreviewPanel);
		htmlPreviewPanel.setWidth("");
		
		html = new HTML("<html>html</html>", true);
		htmlPreviewPanel.add(html);
		
		textArea.addKeyUpHandler(new KeyUpHandler(){

			@Override
			public void onKeyUp(KeyUpEvent event) {
				update();
			}
			
		});
		
		textArea.addValueChangeHandler(new ValueChangeHandler<String>(){

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				update();
			}
			
		});
	}
	protected void update() {
		GWTGenerator.dataService.convertHTMLtoGWT(this.getTextArea().getValue(), new AsyncCallback<GWTData>(){

			@Override
			public void onFailure(Throwable caught) {
				getHtml().setHTML("error. "+caught.getMessage());
				getCode().setHTML("error. "+caught.getMessage());
			}

			public void onSuccess(GWTData result) {
				getHtml().setHTML(result.getHtml());
				getCode().setHTML("<pre>"+clean(result.getCode())+"</pre>");
			}
			
		});
	}
	protected String clean(String html) {
		String retval = "";
		if(html == null) return "";
		for(int i = 0; i < html.length();i++){
			char c = html.charAt(i);
			switch(c){
			case '<':
				retval += "&lt;";
				break;
			case '>':
				retval += "&gt;";
				break;
			case '&':
				retval += "&amp;";
				break;
			default:
				retval += c;
			}
		}
		return retval;
	}
	public HTML getHtml() {
		return html;
	}
	public TextArea getTextArea() {
		return textArea;
	}
	public HTML getCode() {
		return code;
	}
	public SplitLayoutPanel getSplitLayoutPanel() {
		return splitLayoutPanel;
	}
}
